/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu  flash基础类
 * @author  bowen liu | bowenliu@sohu-inc.com
 * @version 1.0 | 2012-06-22
 * @param
 * @example
 */
LJS.define('mod.flvFlash',function(ljs){
	
	// 需要引用的外部js
	//var _parseParam = require('../mod/parseParam').parseParam;
	
	
	var $E = function(obj){
		return document.getElementById(obj)||obj||null
	}
	
	
	// flash player版本检测
	var ver = (function (){
	    var flashVer = {
	      varMajor : -1,
	      varMinor : -1,
	      varRes : -1
	    };
    if(navigator.appName.indexOf("Microsoft")!=-1){
      try{
        var flashIE = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
        flashVerInfo = flashIE.GetVariable("$version");
        var tempArrayMajor = flashVerInfo.split(",");
        var versionMajor = tempArrayMajor[0].split(" ")[1];
        var versionMinor = tempArrayMajor[1];
        var versionRevision = tempArrayMajor[2];
        flashVer.varMajor = versionMajor;
        flashVer.varMinor = versionMinor;
        flashVer.varRes = versionRevision;
      }catch(e){}
    } else {
      try {
        if (navigator.plugins != null && navigator.plugins.length > 0) {
          if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
            var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
            var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
            var descArray = flashDescription.split(" ");
            var tempArrayMajor = descArray[2].split(".");
            var versionMajor = tempArrayMajor[0];
            var versionMinor = tempArrayMajor[1];
            var versionRevision = descArray[3];
            if (versionRevision == "") {
              versionRevision = descArray[4];
            }
            if (versionRevision[0] == "d") {
              versionRevision = versionRevision.substring(1);
            }
            else 
              if (versionRevision[0] == "r") {
                versionRevision = versionRevision.substring(1);
                if (versionRevision.indexOf("d") > 0) {
                  versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
                }
              }
            flashVer.varMajor = versionMajor;
            flashVer.varMinor = versionMinor;
            flashVer.varRes = versionRevision;
          }
        }
      } catch(e){}
    }
    return flashVer;
  })();
  
  //  flash 底层
  var _flash = (function(){
  	
	function getSWF(sURL, oOpts){
        var cfg = {
            'id': 'swf_' + parseInt(Math.random() * 10000, 10),
            'width': 1,
            'height': 1,
            'attrs': {},
            'paras': {},
            'flashvars': {},
            'style':{},
            'html': ''
        };
        //$.core.obj.parseParam(cfg, oOpts);
		//_parseParam(cfg, oOpts)
		$.extend(cfg,oOpts)
        
        if (sURL == null) {
            throw 'swf: [sURL] 未定义';
            return;
        }
        var key;
        var html = [];
        var attrs = [];
        for (key in cfg.attrs) {
            attrs.push(key + '="' + cfg.attrs[key] + '" ');
        }
        
        var flashvars = [];
        for (key in cfg.flashvars) {
            flashvars.push(key + '=' + cfg.flashvars[key]);
        }
        cfg.paras['flashvars'] = flashvars.join('&');
        
        var styles = [];
        for (key in cfg.style) {
            styles.push(key + ':' + cfg.style[key]);
        }
        //cfg.paras['style'] = styles.join(';');
        
        if ($.browser.msie) {
            // 头部
            html.push('<object width="' +
            cfg.width +
            '" height="' +
            cfg.height +
            '" id="' +
            cfg.id +
            '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ');
            
            html.push('style="'+styles.join(';')+'" ');
            // 属性写入
            html.push(attrs.join(''));
            
            // 结束属性标签并为IE写入url(兼容)
            html.push('><param name="movie" value="' + sURL + '" />');
            
            // 参数写入(FlashVars已经在前文填入到参数里了)
            for (key in cfg.paras) {
                html.push('<param name="' + key + '" value="' + cfg.paras[key] + '" />');
            }
            html.push('</object>');
            
        }
        else {
            // 头部
            html.push('<embed width="' +
            cfg.width +
            '" height="' +
            cfg.height +
            '" id="' +
            cfg.id +
            '" src="' +
            sURL +
            '" type="application/x-shockwave-flash" ');
            
            html.push('style="'+styles.join(';')+'" ');
            // 属性写入
            html.push(attrs.join(''));
            
            // 参数写入(FlashVars已经在前文填入到参数里了)
            for (key in cfg.paras) {
                html.push(key + '="' + cfg.paras[key] + '" ');
            }
            
            html.push(' />');
        }
        cfg.html = html.join('');
        return cfg;
    }
    var that = {};
    that.create = function(sNode, sURL, oOpts){
        
        var swf_info = getSWF(sURL, oOpts);
        
        if(!sNode){
            $(document.body).append(swf_info.html);
        }else{
            var oElement = $E(sNode);
            if (oElement == null) {
                throw 'swf: [' + sNode + '] 未找到';
                return;
            }
            oElement.innerHTML = swf_info.html;
        }
        return $E(swf_info.id);
    };
    that.html = function(sURL, oOpts){
        var swf_info = getSWF(sURL, oOpts);
        return swf_info.html;
    };
	return that;
  })();
    function Flash (box,url,op,v){
        this.box = $E(box) || box;
        this.url = url;
        this.v = v || '10.2';
        this.id = op.id || 'f_'+new Date().getTime();
        this.op = op;
        this.errorFlash = '您还未安装Adobe flashplayer，请先安装后上传。<a href="http://get.adobe.com/cn/flashplayer/" onclick="postStart=true" target="_blank">下载并安装</a>',
        this.errorVflash ='您安装的Adobe flashplayer 版本过低，请安装最新版本后上传。<a href="http://get.adobe.com/cn/flashplayer/" onclick="postStart=true" target="_blank">下载并安装</a>',
      
        this.onErrorVersion = [];  // 错误flash版本 
        this.onErrorFlashPlayer = [] // 错误flashplayer
        };
    Flash.prototype = {
        create:function(){
             var check = this.checkFlashActiveX(),_box = this.box, v = this.v.split('.'),that = this;
              //判断是否有容器
             //if(!_box) return
             
              //判断flashplayer是否可用
              if(!check.i_flash&&_box){
                  _box.innerHTML = this.errorFlash;
                  //  执行不可用事件
//                  if($.core.func.getType(this['onErrorFlashPlayer'])=='array'){
//                        $.core.arr.foreach(this['onErrorFlashPlayer'],function(a,i){
//                                                    this['onErrorFlashPlayer']()
//                                                    })
//                        }
                   if($.isArray(this['onErrorFlashPlayer'])){
				   	    $.each(this['onErrorFlashPlayer'],function(){
							that['onErrorFlashPlayer']();
						})
				   } 
                  return
                  }
              // 判断版本是否可用  
              if(check.i_flash&&(ver.varMajor < parseInt(v[0])||(ver.varMajor == parseInt(v[0]) && (ver.varMinor == parseInt(v[1]) && ver.varRes < parseInt(v[2]))))&&_box){
                   _box.innerHTML = this.errorVflash;
                   
                   // 执行版本错误事件
//                   if($.core.func.getType(this['onErrorVersion'])=='array'){
//                        $.core.arr.foreach(this['onErrorVersion'],function(a,i){
//                                                this['onErrorVersion']()
//                                                })
//                    }
                   if($.isArray(this['onErrorFlashPlayer'])){
                        $.each(this['onErrorFlashPlayer'],function(){
                            that['onErrorFlashPlayer']();
                        })
                   } 
                   return
                  }
            // 调用flash
           // STK.core.util.swf.create(this.box,this.url,this.op);
			_flash.create(this.box,this.url,this.op)
            },
        decToHex:function (dec){
            var hexa = "0123456789ABCDEF";
            var hex = "";
            while (dec > 15)
            {
                tmp = dec - (Math.floor(dec / 16)) * 16;
                hex = hexa.charAt(tmp) + hex;
                dec = Math.floor(dec / 16);
            }
            hex = hexa.charAt(dec) + hex;
            return(hex);
        },
        checkFlashActiveX:function(){
            var g_clientInfo = {};
            var i_flash=false;
            var v_flash=false;
            if (window.navigator.plugins) {
                for (var i=0; i < window.navigator.plugins.length; i++) {
                    if (window.navigator.plugins[i].name.toLowerCase().indexOf("shockwave flash") >= 0) {
                        i_flash = true;
                        v_flash = window.navigator.plugins[i].description.substring(window.navigator.plugins[i].description.toLowerCase().lastIndexOf("flash ") + 6, window.navigator.plugins[i].description.length);
                    }
                }
            }
            try{
                //for IE
                var  f = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
                if (f) {
                    i_flash = true
                    v_flash = this.decToHex(f.FlashVersion())
                }
            }catch(e){}
            return g_clientInfo={i_flash:i_flash,v_flash:v_flash}
            
            },
        getFlash:function(){
            return $E(this.id) || null
            }
        }
	
	return Flash
	
});